"""
Case Type   : 兼容weight_string函数
Case Name   : 测试不同字符集下weight_string函数
Create At   : 2023.05.04
Owner       : z@wan005
Description :
    1.创建兼容b库
    2.设置客户端编码为client_encoding
    3.设置客户端编码为gbk
    4.设置客户端编码为LATIN1
    5.设置客户端编码为utf8
    6.设置客户端编码为WIN874
    7.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.成功
    6.报错
    7.成功
History     :--modified by zhanghuan96，2024-5-25，返回结果中的字母变为大写，
            修改用例结果
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class GsDump0010(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.dbuser_node = Node('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = "db_dump_0010"

    def test_server_tools(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        text = '----step1: 创建兼容b库 expect: 成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_result)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_result,
                      '执行失败:' + text)

        text = '----step2: 设置客户端编码为client_encoding expect: 成功----'
        self.log.info(text)
        sql_cmd = f"set client_encoding to SQL_ASCII;" \
                  f"select hex(weight_string(convert(E'\\x57\\x65\\x69\\x67" \
                  f"\\x68\\x74\\x20\\x53\\x74\\x72\\x69\\x6e\\x67\\xe6\\xb5" \
                  f"\\x8b\\xe8\\xaf\\x95\\x31' using sql_ascii) " \
                  f"as char(20)));"
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname=self.db_name)
        self.log.info(sql_result)
        self.assertIn('SET', sql_result, '执行失败:' + text)
        self.assertIn('00570045004900470048005400200053005400520049004E00476D'
                      '4B8BD500310020002000200020', sql_result,
                      '执行失败:' + text)

        text = '----step3: 设置客户端编码为gbk expect: 成功----'
        self.log.info(text)
        sql_cmd = f"set client_encoding to gbk;" \
                  f"select hex(weight_string(convert(E'\\x57\\x65\\x69\\x67" \
                  f"\\x68\\x74\\x20\\x53\\x74\\x72\\x69\\x6e\\x67\\xe6\\xb5" \
                  f"\\x8b\\xe8\\xaf\\x95\\x31' using gbk) " \
                  f"as char(20)));"
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname=self.db_name)
        self.log.info(sql_result)
        self.assertIn('SET', sql_result, '执行失败:' + text)
        self.assertIn('00570045004900470048005400200053005400520049004E00475A'
                      '345B2D00200020002000200020', sql_result,
                      '执行失败:' + text)

        text = '----step4: 设置客户端编码为LATIN1 expect: 成功----'
        self.log.info(text)
        sql_cmd = f"set client_encoding to LATIN1;" \
                  f"select hex(weight_string(convert(E'\\x57\\x65\\x69\\x67" \
                  f"\\x68\\x74\\x20\\x53\\x74\\x72\\x69\\x6e\\x67\\xe6\\xb5" \
                  f"\\x8b\\xe8\\xaf\\x95\\x31' using LATIN1) " \
                  f"as char(20)));"
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname=self.db_name)
        self.log.info(sql_result)
        self.assertIn('SET', sql_result, '执行失败:' + text)
        self.assertIn('00570045004900470048005400200053005400520049004E004700'
                      'E600B5008B0020002000200020', sql_result,
                      '执行失败:' + text)

        text = '----step5: 设置客户端编码为utf8 expect: 成功----'
        self.log.info(text)
        sql_cmd = f"set client_encoding to utf8;" \
                  f"select hex(weight_string(convert(E'\\x57\\x65\\x69\\x67" \
                  f"\\x68\\x74\\x20\\x53\\x74\\x72\\x69\\x6e\\x67\\xe6\\xb5" \
                  f"\\x8b\\xe8\\xaf\\x95\\x31' using utf8) " \
                  f"as char(20)));"
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname=self.db_name)
        self.log.info(sql_result)
        self.assertIn('SET', sql_result, '执行失败:' + text)
        self.assertIn('00570045004900470048005400200053005400520049004E00476D'
                      '4B8BD500310020002000200020', sql_result,
                      '执行失败:' + text)

        text = '----step6: 设置客户端编码为WIN874 expect: 报错----'
        self.log.info(text)
        sql_cmd = f"set client_encoding to WIN874;" \
                  f"select hex(weight_string(convert(E'\\x57\\x65\\x69\\x67" \
                  f"\\x68\\x74\\x20\\x53\\x74\\x72\\x69\\x6e\\x67\\xe6\\xb5" \
                  f"\\x8b\\xe8\\xaf\\x95\\x31' using WIN874) " \
                  f"as char(20)));"
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname=self.db_name)
        self.log.info(sql_result)
        self.assertIn('SET', sql_result, '执行失败:' + text)
        self.assertIn('ERROR:  character with byte sequence 0x8b in encoding '
                      '"WIN874" has no equivalent in encoding "UTF8"',
                      sql_result, '执行失败:' + text)

    def tearDown(self):
        text = '---step7: 清理环境 expect: 成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};"
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname='postgres')
        self.log.info(sql_result)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, sql_result,
                      '执行失败:' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
